<?php


@include_once('./database.php');


$kw = $_GET['kw'];

$sortName = $_GET['sortName'];

$sortType = $_GET['sortType'];


// 前端传第几页，显示几条数据
$page = $_GET['page'];
$showSize = $_GET['showSize'];
$type = $_GET['type'];



// 0,5  5,5  10,5    (page-1) * 5
$start = ($page - 1) * $showSize;


$obj = [];



if ($type) {
    $sql1 = "select count(*) pageAll from goods_list where goods_type like '$type' and goods_des like '%$kw%' order by $sortName $sortType";
    $sql2 = "select * from goods_list where goods_type like '$type' and goods_des like '%$kw%' order by $sortName $sortType limit $start , $showSize ";
} else {
    $sql1 = "select count(*) pageAll from goods_list where goods_des like '%$kw%' order by $sortName $sortType";
    $sql2 = "select * from goods_list where goods_des like '%$kw%' order by $sortName $sortType limit $start , $showSize";
}



// if($type) {
//     $sql1 = "select count(*) pageAll from goods_list where goods_type = '$type'" ;
//     $sql2 = "select * from goods_list where goods_type = '$type' limit $start , $showSize ";
// } else {
//     $sql1 = "select count(*) pageAll from goods_list" ;
//     $sql2 = "select * from goods_list limit $start , $showSize";
// }




$res = mysqli_query($connect, $sql1);
$arr = mysqli_fetch_array($res);
$pageAll = $arr['pageAll'];

// $sql = "select * from goods_list where goods_des like '%$kw%' order by $sortName $sortType ";

// $sql = "select * from goods_list where goods_des like '%$kw%' order by $sortName $sortType";
// 默认只会转第一条数据
$res = mysqli_query($connect, $sql2);

$list = [];

while ($data = mysqli_fetch_array($res)) {
    $temp = [];
    $temp['id'] = $data['goods_id'];
    $temp['name'] = $data['goods_name'];
    $temp['title'] = $data['goods_title'];
    $temp['des'] = $data['goods_des'];
    $temp['img'] = $data['goods_img'];
    $temp['price'] = $data['goods_price'];
    $temp['sales'] = $data['goods_sales'];
    $temp['type'] = $data['goods_type'];
    array_push($list, $temp);
}

if ($list) {
    $obj['status'] = true;
    $obj['msg'] = '数据请求成功';
    $obj['data'] = $list;
    $obj['pageAll'] = $pageAll ;
} else {
    $obj['status'] = false;
    $obj['msg'] = '服务器错误';
    $obj['data'] = $list;
    // $obj['aaa'] = $type;
}

echo (json_encode($obj, JSON_UNESCAPED_UNICODE));
